#include <all_wlib.h>
#pragma hdrstop
//#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "HWindow"

#include "ozf_Base.h"

//---------------------------------------------------------------------------
//  TOziBaseForm
//---------------------------------------------------------------------------
__fastcall TOziBaseForm::TOziBaseForm( TComponent* Owner )
    : TForm(Owner),
      FormPtr( NULL )
  {
     FRealValues = false;
     isMax       = false;
     wKey[0]     = 0;

     OziRegisterEvent( idOZIPos );
}

void __fastcall TOziBaseForm::idOZIPos( double Lat,double Lon,double vSpeed,double Head,double Mag,double Alt, int x, int y )
  {
     FRealValues = true;
     idPos( Lat,Lon,vSpeed,Head,Mag,Alt, x, y );
     FRealValues = false;
}

void __fastcall TOziBaseForm::SetCaption( CONSTSTR m ) { client->BorderTop->Caption = m; }
void __fastcall TOziBaseForm::idPos( double Lat,double Lon,double vSpeed,double Head,double Mag,double Alt, int x, int y ) {}

void __fastcall TOziBaseForm::CreateParams(TCreateParams &Params)
  {
     TForm::CreateParams(Params);
     Params.Style = WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_POPUP | WS_THICKFRAME;
}

void __fastcall TOziBaseForm::WndProc( Messages::TMessage &m )
  {
    if ( m.Msg == WM_ERASEBKGND )
      m.Result = TRUE;
     else
      TForm::WndProc( m );

    if ( HandleAllocated() )
      client->Active = Active;
}
//---------------------------------------------------------------------------
void __fastcall TOziBaseForm::idOnButton(TObject *Sender,
      const PHCustomWindowBorder Border, const HCaptionButton_t Button)
  {
     if ( Button == hcbMaximize ) {
       if ( !isMax ) {
         NormSize.Set( Left, Top, Width, Height );
         NormButtons = client->BorderTop->Buttons;
         SetBounds( 0, 0, GetSystemMetrics( SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ) );
         client->ActionsOnParent    = HCaptionButtons() << hcbClose;
         client->BorderTop->Buttons = HCaptionButtons() << hcbClose << hcbMaximize;
       } else {
         SetBounds( NormSize.x, NormSize.y, NormSize.x1, NormSize.y1 );
         client->ActionsOnParent    = HCaptionButtons() << hcbClose << hcbRoller << hcbResize << hcbCaption;
         client->BorderTop->Buttons = NormButtons;
       }

       isMax = !isMax;
     }
}
//---------------------------------------------------------------------------
void __fastcall TOziBaseForm::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
  {  int num;

     if ( Shift == (TShiftState() << ssAlt) ) {
       if ( (num=StrNChr("1234567890",Key)) != -1 )
         PostMessage( MainWindow, WM_USER+num, 0, 0 );
        else
       if ( Key == VK_OEM_3 )
         PostMessage( MainWindow, WM_USER+1024, 0, 0 );
     }
}
//---------------------------------------------------------------------------
void __fastcall TOziBaseForm::FormCreate(TObject *Sender)
  {
    //SetWindowLong( Handle, GWL_HWNDPARENT, NULL );

}

//---------------------------------------------------------------------------
void __fastcall TOziBaseForm::Init( CONSTSTR key, PTForm *ptr )
  {
     FormPtr = ptr;
     SNprintf( wKey, sizeof(wKey), "%s", key );

     client->BorderTop->Size = GOPT( "TitleHeight", client->BorderTop->Size );

     PHConfigItem cfg = Opt->Key->ConfigGetCreate(wKey);

    //Load pos
     HLoadFormPos( cfg->ConfigGetCreate( "Window" ), this );

    //Load overrides
     Overrides.Load( this, cfg->ConfigGetCreate( "Override" ) );
}

//---------------------------------------------------------------------------
void __fastcall TOziBaseForm::FormClose(TObject *Sender,TCloseAction &Action)
  {
     PHConfigItem cfg = Opt->Key->ConfigGetCreate(wKey);

     //Save pos
     HSaveFormPos( cfg->ConfigGetCreate( "Window" ), this );

     //Save overrides
     Overrides.Save( cfg->ConfigGetCreate( "Override" ) );

     OziUnregisterEvent( idPos );
     if (FormPtr) *FormPtr = NULL;
}
//---------------------------------------------------------------------------

